# import subprocess
# import sys
# import time
# import random
# import os
# import re
# def split_text2(text, length):
#     text_arr = re.findall(r'.{%d}' % int(length), text)
#     return text_arr  # ['123', '456', '789', 'abc', 'def']

# if __name__ == '__main__':
#     cmdLine = []
#     p = 0
#     cmdLine.append('sudo')
#     cmdLine.append('btmon')
#     tmpFile = "data.txt" #临时生成一个文件
#     fpWrite = open(tmpFile,'w')
#     process = subprocess.Popen(cmdLine,stdout = fpWrite,stderr = subprocess.PIPE);
#     while True:
#         fpRead = open(tmpFile,'r')  #这里又重新创建了一个文件读取对象，不知为何，用上面的就是读不出来，改w+也不行
#         lines = fpRead.readlines()
#         for line in lines:
#             if "Data:" in line:
#                 judge = False
#                 line = line.strip('\n')
#                 data = line.split('Data:')
#                 for str in data:
#                     sub_str = str.split(' ')
#                 if sub_str:
#                     temp = split_text2(sub_str[1], 4)
#                     mydata = []
#                     for i in range(len(temp)):
#                         if temp[i] not in mydata:
#                             mydata.append(temp[i])
#                     if len(mydata) < 3:
#                         continue
#                     if mydata[0] == "aa55":
#                         judge = True
#                     if judge:
#                         judge = False
#                         if ord(mydata[1][2]) >= 97:
#                             light1 = ((ord(mydata[1][2]) - 87) << 4)
#                         else:
#                             light1 = int(mydata[1][2]) << 4
#                         if ord(mydata[1][3]) >= 97:
#                             light2 = (ord(mydata[1][3]) - 87)
#                         else:
#                             light2 = int(mydata[1][3])
#                         if ord(mydata[2][2]) >= 97:
#                             temperature1 = ((ord(mydata[2][2]) - 87) << 4)
#                         else:
#                             temperature1 = int(mydata[2][2]) << 4
#                         if ord(mydata[2][3]) >= 97:
#                             temperature2 = (ord(mydata[2][3]) - 87)
#                         else:
#                             temperature2 = int(mydata[2][3])
#                         light = light1 + light2
#                         temperature = temperature1 + temperature2
#                     p += 1
#                     print(" [-%d-] light：%d，temperature：%d℃" % (p, light, temperature))
#         if process.poll():
#             break
#         fpWrite.truncate()  #此处清空文件，等待记录下一次输出的进度
#         fpRead.close()
#         time.sleep(3)
#     fpWrite.close()
#     error = process.stderr.read()
#     if not error == None:
#         print ('error info:%s' % error)
#     os.popen('rm -rf %s' % tmpFile) #删除临时文件
#     print("finished")

# run.py

from subprocess import *
import threading
import time
import re

process = Popen('sudo btmon', shell=True, stdin=PIPE, stdout=PIPE)
p = 0
light = 0
temperature = 0

def split_text2(text, length):
    text_arr = re.findall(r'.{%d}' % int(length), text)
    return text_arr  # ['123', '456', '789', 'abc', 'def']


def run():
    global process, p, sub_str,light,temperature
    while True:
        line = process.stdout.readline()
        if not line:  # 空则跳出
            break
        line = str(line.strip()).strip('b\'')
        if "Data:" in line:
            print(line)
            line = line.strip('\n')
            data = line.split('Data: ')
            temp = split_text2(data[1], 2)
            if ord(temp[0][0]) >= 97:
                light1 = ((ord(temp[0][0]) - 87) << 4)
            else:
                light1 = int(temp[0][0]) << 4
            if ord(temp[0][1]) >= 97:
                light2 = (ord(temp[0][1]) - 87)
            else:
                light2 = int(temp[0][1])
            if ord(temp[1][0]) >= 97:
                temperature1 = ((ord(temp[1][0]) - 87) << 4)
            else:
                temperature1 = int(temp[1][0]) << 4
            if ord(temp[1][1]) >= 97:
                temperature2 = (ord(temp[1][1]) - 87)
            else:
                temperature2 = int(temp[1][1])
            
            light = light1 + light2
            temperature = temperature1 + temperature2
        p += 1
        if p > 200:
            p = 0
        print(" [-%d-] light：%d，temperature：%d℃" % (p,light,temperature))


w = threading.Thread(target=run)
process.stdin.flush()
time.sleep(1)  # 延迟是因为等待一下线程就绪
process.stdin.flush()
w.start()
